package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlAttendance;
import com.ruoyi.sl.domain.SlOddjob;
import com.ruoyi.sl.domain.SlRwork;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlOddjobAttendance;
import com.ruoyi.sl.service.ISlOddjobAttendanceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 零工出勤天数Controller
 * 
 * @author admin
 * @date 2024-06-16
 */
@RestController
@RequestMapping("/sl/oddjobattendance")
public class SlOddjobAttendanceController extends BaseController
{
    @Autowired
    private ISlOddjobAttendanceService slOddjobAttendanceService;

    /**
     * 查询零工出勤天数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlOddjobAttendance slOddjobAttendance)
    {
        startPage();
        List<SlOddjobAttendance> list = slOddjobAttendanceService.selectSlOddjobAttendanceList(slOddjobAttendance);
        return getDataTable(list);
    }

    /**
     * 导出零工出勤天数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:export')")
    @Log(title = "零工出勤天数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlOddjobAttendance slOddjobAttendance)
    {
        List<SlOddjobAttendance> list = slOddjobAttendanceService.selectSlOddjobAttendanceList(slOddjobAttendance);
        ExcelUtil<SlOddjobAttendance> util = new ExcelUtil<SlOddjobAttendance>(SlOddjobAttendance.class);
        util.exportExcel(response, list, "零工出勤天数数据");
    }

    /**
     * 根据报工日期和报工人查询出勤列表
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:list')")
    @PostMapping("/all")
    public AjaxResult getAllOddjobAttendance(@RequestBody SlOddjob slOddjob)
    {
        return success(slOddjobAttendanceService.selectSlOddjobAttendanceByOddjob(slOddjob));
    }


    /**
     * 获取零工出勤天数详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:query')")
    @GetMapping(value = "/{attendanceId}")
    public AjaxResult getInfo(@PathVariable("attendanceId") Long attendanceId)
    {
        return success(slOddjobAttendanceService.selectSlOddjobAttendanceByAttendanceId(attendanceId));
    }

    /**
     * 新增零工出勤天数
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:add')")
    @Log(title = "零工出勤天数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlOddjobAttendance slOddjobAttendance)
    {
        return toAjax(slOddjobAttendanceService.insertSlOddjobAttendance(slOddjobAttendance));
    }

    /**
     * 修改零工出勤天数
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:edit')")
    @Log(title = "零工出勤天数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlOddjobAttendance slOddjobAttendance)
    {
        return toAjax(slOddjobAttendanceService.updateSlOddjobAttendance(slOddjobAttendance));
    }

    /**
     * 修改出勤天数list
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:edit')")
    @Log(title = "零工出勤天数", businessType = BusinessType.UPDATE)
    @PutMapping("/upal")
    public AjaxResult updateOddjobAttendanceList(@RequestBody List<SlOddjobAttendance> slOddjobAttendanceList)
    {

        if (StringUtils.isNotNull(slOddjobAttendanceList)){
            for (SlOddjobAttendance slOddjobAttendance: slOddjobAttendanceList) {
                slOddjobAttendanceService.updateSlOddjobAttendance(slOddjobAttendance);
            }
        }
        return toAjax(slOddjobAttendanceList.size());
    }

    /**
     * 删除零工出勤天数
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:remove')")
    @Log(title = "零工出勤天数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{attendanceIds}")
    public AjaxResult remove(@PathVariable Long[] attendanceIds)
    {
        return toAjax(slOddjobAttendanceService.deleteSlOddjobAttendanceByAttendanceIds(attendanceIds));
    }
}
